package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/house-robber-ii/">213. 打家劫舍 II</a>
 */
public class Solution213 extends BaseSolution {

    public int rob(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }
        return Math.max(getMax(nums, 0, nums.length - 1), getMax(nums, 1, nums.length));
    }

    public int getMax(int[] nums, int l, int r) {
        int rob = 0, unRob = 0;
        for (; l < r; l++) {
            int tr = unRob + nums[l];
            int tu = Math.max(rob, unRob);
            rob = tr;
            unRob = tu;
        }
        return Math.max(rob, unRob);
    }

    public static void main(String[] args) {
        Solution213 solution = new Solution213();
        System.out.println(solution.rob(new int[] {
                1
        }));
    }
}